package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.FileManageAdapter;
import com.isunland.managesystem.base.BaseConfirmDialogFragment;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.utils.FileUtil;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/5/22 15:34
 */
public class FileManageListFragment extends BaseListFragment implements FileManageAdapter.Callback {
  private static final int MENU_ITEM_CLEAR = 1;
  private static final int REQUEST_DELETE = 2;
  private static final int REQUEST_CLEAR = 3;
  private ArrayList<File> mList;
  private FileManageAdapter mAdapter;
  /** 待删除文件 */
  private File mDeleteFile;

  @Override public String getUrl() {
    return null;
  }

  @Override public void resolveResponse(String json) {

  }

  @Override public void initData() {
    super.initData();
    setIsForbiddenVolley(true);//禁用volley
    mList = new ArrayList<>();
    refreshFile();
    mAdapter = new FileManageAdapter(mActivity, mList, this);
    mAdapter.setmIconRes(R.drawable.ic_file_blue_128);
  }

  private void refreshFile() {
    File[] result = FileUtil.orderByDate(FileUtil.getFileDir());
    if (result == null) result = new File[0];
    mList.clear();
    mList.addAll(new ArrayList<>(Arrays.asList(result)));
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setTitleCustom(R.string.fileManage);
    setListAdapter(mAdapter);
    //禁用下拉刷新
    getPullRefreshListView().setMode(PullToRefreshBase.Mode.DISABLED);
    showViewByType(SHOW_EMPTY_VIEW);//否则会显示loading.
  }

  @Override public void delete(File item) {
    if (item == null) return;
    mDeleteFile = item;
    showDialog(BaseConfirmDialogFragment.newInstance("是否删除文件?  \n " + item.getName()),
        REQUEST_DELETE);
  }

  @Override public void check(File item) {
    FileUtil.openFile(item, mActivity);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    menu.add(Menu.NONE, MENU_ITEM_CLEAR, 0, getString(R.string.clearChoose))
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == MENU_ITEM_CLEAR) {
      showDialog(BaseConfirmDialogFragment.newInstance("是否删除所有文件?"), REQUEST_CLEAR);
    }
    return super.onOptionsItemSelected(item);
  }

  private void notifyAdapter() {
    refreshFile();
    mAdapter.notifyDataSetChanged();
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_DELETE) {
      deleteFile(mDeleteFile);
    }
    if (requestCode == REQUEST_CLEAR) {
      deleteAllFile(new File(FileUtil.getFileDir()));
    }
  }

  private void deleteFile(File file) {
    if (file == null) return;
    boolean isSuccess = FileUtil.deleteFile(file);
    if (isSuccess) notifyAdapter();
    ToastUtil.show(
        MyStringUtil.connect(R.string.delete, isSuccess ? R.string.success : R.string.failure));
  }

  private void deleteAllFile(File file) {
    if (file == null || !file.isDirectory()) return;
    File[] files = file.listFiles();
    for (File subFile : files) {
      FileUtil.deleteFile(subFile);
    }
    notifyAdapter();
  }
}

